package com.iwanghang.movelineview.whCustomView;

import android.graphics.Point;

import com.iwanghang.movelineview.whUtil.Tools;

import static java.lang.Math.abs;

public class MoveFoldLineUtil {

    public static Point[] getPoints(final Point[] foldPoints, final int speed) {

        int pointsLength = 0;
        // 通过起点终点，根据y的数量，计算直线经过的点
        for (int i = 0; i < foldPoints.length - 1; i++) {
            float startX = foldPoints[i].x;
            float startY = foldPoints[i].y;
            float endX = foldPoints[i + 1].x;
            float endY = foldPoints[i + 1].y;
            Tools.logByWh("MoveFoldLineUtil - startX = " + startX + " - startY = " + startY + " - endX = " + endX + " - endY = " + endY);

            // 当前线y的数量
            int pointsLengthThis = abs((int) (endY-startY)) / speed + 1;

            // 累加所有线y的数量
            pointsLength =  pointsLength + pointsLengthThis;

            Tools.logByWh("MoveFoldLineUtil - pointsLengthThis = " + pointsLengthThis + " - pointsLength = " + pointsLength);
        }

        Point[] points = new Point[pointsLength + foldPoints.length - 1];
        int zMark = 0;
        for (int i = 0; i < foldPoints.length - 1; i++) {
            float startX = foldPoints[i].x;
            float startY = foldPoints[i].y;
            float endX = foldPoints[i + 1].x;
            float endY = foldPoints[i + 1].y;
            Tools.logByWh("MoveFoldLineUtil - startX = " + startX + " - startY = " + startY + " - endX = " + endX + " - endY = " + endY);

            // 当前线y的数量
            int pointsLengthThis = abs((int) (endY-startY)) / speed + 1;

            // 当前线的x的宽度
            float width = abs((int) (endX-startX));
            // 当前线的y的高度
            float height = abs((int) (endY-startY));

            float xPadding = width / pointsLengthThis;  // x间距
            Tools.logByWh("pointsLengthThis = " + pointsLengthThis + " - width = " + width + " - xPadding = " + xPadding);

            float yPadding = height / pointsLengthThis;  // y间距
            Tools.logByWh("pointsLengthThis = " + pointsLengthThis + " - height = " + height + " - yPadding = " + yPadding);


            Tools.logByWh("MoveFoldLineUtil - zMark = !!!");

            for (int z = 0; z < pointsLengthThis + 1; z++) {

                int pointX;
                if (endX > startX) {
                    pointX = (int) (startX + xPadding * z);
                } else {
                    pointX = (int) (startX - xPadding * z);
                }

                int pointY;
                if (endY > startY) {
                    pointY = (int) (startY + z * yPadding);
                } else {
                    pointY = (int) (startY - z * yPadding);
                }

                if (z == pointsLengthThis){
                    pointX = (int) endX;
                    pointY = (int) endY;
                }

                Tools.logByWh("MoveFoldLineUtil - z = " + z + " - pointsLengthThis = " + pointsLengthThis);
                Tools.logByWh("MoveFoldLineUtil - zMark = " + zMark + " - pointX = " + pointX + " - pointY = " + pointY);
                Point point = new Point(pointX, pointY);
                points[zMark] = point;
                zMark = zMark + 1;
            }
        }
        return points;
    }

}
